package net.xdclass.onlinexdclass.templates;

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * RestTemplate工具类
 */
@Component
public class RestTemplateUtil {

    private static class SingletonRestTemplate {
        static final RestTemplate INSTANCE = new RestTemplate();
    }

    private RestTemplateUtil() {

    }

    public static RestTemplate getInstance() {
        return SingletonRestTemplate.INSTANCE;
    }


    /**
     * post 请求
     *
     * @param url   请求路径
     * @param data  body数据
     * @param token JWT所需的Token，不需要的可去掉
     * @return
     */
    public static String post(String url, String data, String token) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Accept", "application/json");
        httpHeaders.add("Content-Encoding", "UTF-8");
        httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
        if (token != null) {
            httpHeaders.add("Authorization", token);
        }
        HttpEntity<String> requestEntity = new HttpEntity<>(data, httpHeaders);
        return RestTemplateUtil.getInstance().postForEntity(url, requestEntity, String.class).getBody();
    }


    /**
     * get请求
     *
     * @param url   请求路径
     * @param token JWT所需的Token, 不需要的可去掉
     * @return
     */
    public static String get(String url, String token) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Accept", "application/json");
        httpHeaders.add("Content-Encoding", "UTF-8");
        httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
        if (null != token) {
            httpHeaders.add("Authorization", token);
        }

        HttpEntity<String> requestEntity = new HttpEntity<>(null, httpHeaders);

        ResponseEntity<String> exchange = RestTemplateUtil.getInstance().exchange(url, HttpMethod.GET, requestEntity, String.class);
        String body = exchange.getBody();
        return body;

    }


    /**
     * 文件传输
     * @param url
     * @param file 要传输的文件
     * @param token
     * @return
     */
    public static String file(String url, MultipartFile file, String token) {
        //生成临时文件  System.getProperty("java.io.tmpdir") : Java输入输出临时路径
        String tempFilePath = System.getProperty("java.io.tmpdir") + file.getOriginalFilename();
        File tmpFile = new File(tempFilePath);
        //结果,抛出异常就是null
        String result = null;

        try {
            //保存为文件,将临时传过来的文件，覆盖临时目录下的文件
            file.transferTo(tmpFile);
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
            httpHeaders.setContentType(MediaType.parseMediaType("multipart/form-data;charset=UTF-8"));
            if (null != token) {
                httpHeaders.add("Authorization", token);
            }
            MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
            //把临时文件变成FileSystemResource,加载绝对路径资源
            FileSystemResource fileSystemResource = new FileSystemResource(tempFilePath);
            multiValueMap.add("file", fileSystemResource);
            HttpEntity<MultiValueMap<String, Object>> mapHttpEntity = new HttpEntity<>(multiValueMap, httpHeaders);
            result = RestTemplateUtil.getInstance().postForObject(url, mapHttpEntity, String.class);
            //删除临时文件
            tmpFile.delete();


        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }


    private void systemDemo() {
        System.out.println("java版本号：" + System.getProperty("java.version")); // java版本号
        System.out.println("Java提供商名称：" + System.getProperty("java.vendor")); // Java提供商名称
        System.out.println("Java提供商网站：" + System.getProperty("java.vendor.url")); // Java提供商网站
        System.out.println("jre目录：" + System.getProperty("java.home")); // Java，哦，应该是jre目录
        System.out.println("Java虚拟机规范版本号：" + System.getProperty("java.vm.specification.version")); // Java虚拟机规范版本号
        System.out.println("Java虚拟机规范提供商：" + System.getProperty("java.vm.specification.vendor")); // Java虚拟机规范提供商
        System.out.println("Java虚拟机规范名称：" + System.getProperty("java.vm.specification.name")); // Java虚拟机规范名称
        System.out.println("Java虚拟机版本号：" + System.getProperty("java.vm.version")); // Java虚拟机版本号
        System.out.println("Java虚拟机提供商：" + System.getProperty("java.vm.vendor")); // Java虚拟机提供商
        System.out.println("Java虚拟机名称：" + System.getProperty("java.vm.name")); // Java虚拟机名称
        System.out.println("Java规范版本号：" + System.getProperty("java.specification.version")); // Java规范版本号
        System.out.println("Java规范提供商：" + System.getProperty("java.specification.vendor")); // Java规范提供商
        System.out.println("Java规范名称：" + System.getProperty("java.specification.name")); // Java规范名称
        System.out.println("Java类版本号：" + System.getProperty("java.class.version")); // Java类版本号
        System.out.println("Java类路径：" + System.getProperty("java.class.path")); // Java类路径
        System.out.println("Java lib路径：" + System.getProperty("java.library.path")); // Java lib路径
        System.out.println("Java输入输出临时路径：" + System.getProperty("java.io.tmpdir")); // Java输入输出临时路径
        System.out.println("Java编译器：" + System.getProperty("java.compiler")); // Java编译器
        System.out.println("Java执行路径：" + System.getProperty("java.ext.dirs")); // Java执行路径
        System.out.println("操作系统名称：" + System.getProperty("os.name")); // 操作系统名称
        System.out.println("操作系统的架构：" + System.getProperty("os.arch")); // 操作系统的架构
        System.out.println("操作系统版本号：" + System.getProperty("os.version")); // 操作系统版本号
        System.out.println("文件分隔符：" + System.getProperty("file.separator")); // 文件分隔符
        System.out.println("路径分隔符：" + System.getProperty("path.separator")); // 路径分隔符
        System.out.println("直线分隔符：" + System.getProperty("line.separator")); // 直线分隔符
        System.out.println("操作系统用户名：" + System.getProperty("user.name")); // 用户名
        System.out.println("操作系统用户的主目录：" + System.getProperty("user.home")); // 用户的主目录
        System.out.println("当前程序所在目录：" + System.getProperty("user.dir")); // 当前程序所在目录
    }

}
